package linker.account;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * The class is to manage Account information with the file path used to include
 * extra files like images and plugins
 * 
 * @version linker 0.93
 * @author Jianfeng jianfeng.linker@gmail.com
 * 
 */
public class ManageAccount {

	// The resources fold's path related to this software
	private static String sourcePath;

	private static String imagePath = null;

	// The path of userInfo
	private String infoPath;

	// The refer of self
	private static ManageAccount manageAccount;

	private static String configPath;

	private static String infoDirPath;

	private static String pluginPath;

	// The properties which holds the whole information of accounts
	private Properties accounts = new Properties();

	/**
	 * The constructer of ManagerAccount class
	 * 
	 * Initial the sourcePath
	 * 
	 */
	public ManageAccount() {
		sourcePath = getPath();
	}

	/**
	 * getAccounts() to get Accounts information
	 * 
	 * @return the accounts information
	 */
	public Properties getAccounts() {

		try {
			
			infoPath = sourcePath + "info" + getFileSeparator() + "user.info";

			File info = new File(infoPath);
			if (!info.exists())
				info.createNewFile();

			accounts.load(new FileInputStream(infoPath));

		} catch (Exception e) {
			e.printStackTrace();
		}

		return accounts;

	}

	/**
	 * Add the used account information and store them
	 * 
	 * @param username
	 * @param password
	 * @param save
	 */
	public void setAccount(String username, String password, String host,
			String port, String domain, boolean save) {
		if (save)
			accounts.put(username, password);
		else
			accounts.put(username, "");
		
		if(port!=null)
			accounts.put(username+".port", port);
		if(host!=null)
			accounts.put(username+".host", host);
		if(domain!=null)
			accounts.put(username+".domain", domain);

		try {
			accounts.store(new FileOutputStream(infoPath), "UserInfo");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * get the SourcePath
	 * 
	 * @return
	 */
	public static String getPath() {
		String temp = ManageAccount.class.getResource("").toString();

		if (sourcePath != null)
			return sourcePath;

		// This path is for edit and compile tiem using
		if (temp.indexOf("Linker.jar") == -1)
			return "resources/";

		temp = temp.substring(temp.indexOf("file:") + 5, temp
				.indexOf("Linker.jar"));
		if (System.getProperty("os.name").indexOf("Linux") < 0) {
			temp = temp.substring(1);

			String s = "";
			for (int i = 0; i < temp.length(); i++)
				if (temp.charAt(i) == '/')
					s = s + "\\\\";
				else
					s = s + temp.charAt(i);

			temp = s;
		}

		return temp + "resources" + getFileSeparator();
	}

	/**
	 * Get the right fileseparator of the currend used system
	 * 
	 * @return
	 */
	public static String getFileSeparator() {
		return System.getProperty("file.separator")
				+ System.getProperty("file.separator");
	}

	/**
	 * Get the self of ManageAccount
	 * 
	 * @return
	 */
	public static ManageAccount getManageAccount() {
		if (manageAccount == null)
			manageAccount = new ManageAccount();

		return manageAccount;
	}

	public static String getImagePath() {
		if (imagePath == null)
			imagePath = getPath() + "images" + getFileSeparator();

		return imagePath;

	}

	public static String getConfigPath() {
		if (configPath == null)
			configPath = getPath() + "config" + getFileSeparator();

		return configPath;
	}

	public static String getInfoPath() {
		if (infoDirPath == null)
			infoDirPath = getPath() + "info" + getFileSeparator();

		return infoDirPath;
	}

	public static String getPluginPath() {
		if (pluginPath == null)
			pluginPath = getPath() + "plugins" + getFileSeparator();

		return pluginPath;
	}
}
